package bz.ziro.kanbe.controller.mng.site;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import bz.ziro.kanbe.dao.SiteDao;
import bz.ziro.kanbe.model.Site;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * サイトの更新
 * @author Administrator
 *
 */
public class UpdateController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(UpdateController.class.getName());

    @Override
    public Navigation run() {

        String keyBuf = requestScope("siteKey");
        Site site = SiteDao.find(Long.valueOf(keyBuf));
        //検索する
        BeanUtil.copy(request, site);
        try {
            String publishDate = requestScope("workPublishDate");
            if ( publishDate != null && !publishDate.equals("") ) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            	site.setPublishDate(sdf.parse(publishDate));
            }
        } catch ( Exception ex ) {
        }
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        //作成日を設定
        site.setEditDate(new Date());
        site.setEditor(user);

        Datastore.put(site);

        return null;
    }
}
